package com.fysl.app.ui;

import com.hyphenate.chat.EMCallStateChangeListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMCallManager.EMVideoCallHelper.CallType;
import com.hyphenate.chat.EMMessage.Status;
import com.hyphenate.chat.EMTextMessageBody;
import com.fysl.app.Constant;
import com.fysl.app.R;
import com.hyphenate.exceptions.EMServiceNotReadyException;
import com.hyphenate.media.EMLocalSurfaceView;
import com.hyphenate.media.EMOppositeSurfaceView;
import com.hyphenate.util.EMLog;
import com.hyphenate.util.NetUtils;

import android.content.Context;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.widget.Toast;

public class CallActivity extends BaseActivity {
	protected final int MSG_CALL_MAKE_VIDEO = 0;
	protected final int MSG_CALL_MAKE_VOICE = 1;
	protected final int MSG_CALL_ANSWER = 2;
	protected final int MSG_CALL_REJECT = 3;
	protected final int MSG_CALL_END = 4;
	protected final int MSG_CALL_RLEASE_HANDLER = 5;

	protected boolean isInComingCall;
	protected String username;
	protected CallingState callingState = CallingState.CANCED;
	protected String callDruationText;
	protected String msgid;
	protected AudioManager audioManager;
	protected SoundPool soundPool;
	protected Ringtone ringtone;
	protected int outgoing;
	protected EMCallStateChangeListener callStateListener;
	protected EMLocalSurfaceView localSurface;
	protected EMOppositeSurfaceView oppositeSurface;
	protected boolean isAnswered = false;
	protected int streamID = -1;

	/**
	 * 0：音频，1：视频
	 */
	protected int callType = 0;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		audioManager = (AudioManager) this
				.getSystemService(Context.AUDIO_SERVICE);
	}

	@Override
	protected void onDestroy() {
		if (soundPool != null)
			soundPool.release();
		if (ringtone != null && ringtone.isPlaying())
			ringtone.stop();
		audioManager.setMode(AudioManager.MODE_NORMAL);
		audioManager.setMicrophoneMute(false);

		if (callStateListener != null)
			EMClient.getInstance().callManager()
					.removeCallStateChangeListener(callStateListener);
		releaseHandler();
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		handler.sendEmptyMessage(MSG_CALL_END);
		saveCallRecord();
		finish();
		super.onBackPressed();
	}

	Runnable timeoutHangup = new Runnable() {

		@Override
		public void run() {
			handler.sendEmptyMessage(MSG_CALL_END);
		}
	};

	HandlerThread callHandlerThread = new HandlerThread("callHandlerThread");
	{
		callHandlerThread.start();
	}
	protected Handler handler = new Handler(callHandlerThread.getLooper()) {
		@Override
		public void handleMessage(Message msg) {
			EMLog.d("EMCallManager CallActivity",
					"handleMessage ---enter--- msg.what:" + msg.what);
			switch (msg.what) {
			case MSG_CALL_MAKE_VIDEO:
			case MSG_CALL_MAKE_VOICE:
				try {
					streamID = playMakeCallSounds();
					if (msg.what == MSG_CALL_MAKE_VIDEO) {
						EMClient.getInstance().callManager()
								.makeVideoCall(username);
					} else {
						EMClient.getInstance().callManager()
								.makeVoiceCall(username);
					}

					final int MAKE_CALL_TIMEOUT = 50 * 1000;
					handler.removeCallbacks(timeoutHangup);
					handler.postDelayed(timeoutHangup, MAKE_CALL_TIMEOUT);
				} catch (EMServiceNotReadyException e) {
					e.printStackTrace();
					// runOnUiThread(new Runnable() {
					// public void run() {
					// final String st2 =
					// getResources().getString(R.string.Is_not_yet_connected_to_the_server);
					// Toast.makeText(CallActivity.this, st2,
					// Toast.LENGTH_SHORT).show();
					// }
					// });
				}
				break;
			case MSG_CALL_ANSWER:
				if (ringtone != null)
					ringtone.stop();
				if (isInComingCall) {
					try {
						if (NetUtils.hasDataConnection(CallActivity.this)) {
							EMClient.getInstance().callManager().answerCall();
							isAnswered = true;
						} else {
							runOnUiThread(new Runnable() {
								public void run() {
									final String st2 = getResources()
											.getString(
													R.string.Is_not_yet_connected_to_the_server);
									Toast.makeText(CallActivity.this, st2,
											Toast.LENGTH_SHORT).show();
									CallActivity.this.finish();
								}
							});
							throw new Exception();
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						saveCallRecord();
						finish();
						return;
					}
				}
				break;
			case MSG_CALL_REJECT:
				if (ringtone != null)
					ringtone.stop();
				try {
					EMClient.getInstance().callManager().rejectCall();
				} catch (Exception e1) {
					e1.printStackTrace();
					saveCallRecord();
					finish();
				}
				callingState = CallingState.REFUESD;
				break;
			case MSG_CALL_END:
				if (soundPool != null)
					soundPool.stop(streamID);
				try {
					EMClient.getInstance().callManager().endCall();
				} catch (Exception e) {
					e.printStackTrace();
					saveCallRecord();
					finish();
				}
				break;
			case MSG_CALL_RLEASE_HANDLER:
				EMClient.getInstance().callManager().endCall();
				handler.removeCallbacks(timeoutHangup);
				handler.removeMessages(MSG_CALL_MAKE_VIDEO);
				handler.removeMessages(MSG_CALL_MAKE_VOICE);
				handler.removeMessages(MSG_CALL_ANSWER);
				handler.removeMessages(MSG_CALL_REJECT);
				handler.removeMessages(MSG_CALL_END);
				callHandlerThread.quit();
				break;
			default:
				break;
			}
			EMLog.d("EMCallManager CallActivity",
					"handleMessage ---exit--- msg.what:" + msg.what);
		}
	};

	void releaseHandler() {
		handler.sendEmptyMessage(MSG_CALL_RLEASE_HANDLER);
	}

	/**
	 * 播放拨号响铃
	 * 
	 * @param sound
	 * @param number
	 */
	protected int playMakeCallSounds() {
		try {
			// 最大音量
			float audioMaxVolumn = audioManager
					.getStreamMaxVolume(AudioManager.STREAM_RING);
			// 当前音量
			float audioCurrentVolumn = audioManager
					.getStreamVolume(AudioManager.STREAM_RING);
			float volumnRatio = audioCurrentVolumn / audioMaxVolumn;

			audioManager.setMode(AudioManager.MODE_RINGTONE);
			audioManager.setSpeakerphoneOn(false);

			// 播放
			int id = soundPool.play(outgoing, // 声音资源
					0.3f, // 左声道
					0.3f, // 右声道
					1, // 优先级，0最低
					-1, // 循环次数，0是不循环，-1是永远循环
					1); // 回放速度，0.5-2.0之间。1为正常速度
			return id;
		} catch (Exception e) {
			return -1;
		}
	}

	// 打开扬声器
	protected void openSpeakerOn() {
		try {
			if (!audioManager.isSpeakerphoneOn())
				audioManager.setSpeakerphoneOn(true);
			audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 关闭扬声器
	protected void closeSpeakerOn() {

		try {
			if (audioManager != null) {
				// int curVolume =
				// audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
				if (audioManager.isSpeakerphoneOn())
					audioManager.setSpeakerphoneOn(false);
				audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
				// audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
				// curVolume, AudioManager.STREAM_VOICE_CALL);

			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 保存通话消息记录
	 * 
	 * @param type
	 *            0：音频，1：视频
	 */
	protected void saveCallRecord() {
		EMMessage message = null;
		EMTextMessageBody txtBody = null;
		if (!isInComingCall) { // 打出去的通话
			message = EMMessage.createSendMessage(EMMessage.Type.TXT);
			message.setReceipt(username);
		} else {
			message = EMMessage.createReceiveMessage(EMMessage.Type.TXT);
			message.setFrom(username);
		}

		String st1 = getResources().getString(R.string.call_duration);
		String st2 = getResources().getString(R.string.Refused);
		String st3 = getResources().getString(
				R.string.The_other_party_has_refused_to);
		String st4 = getResources().getString(R.string.The_other_is_not_online);
		String st5 = getResources().getString(
				R.string.The_other_is_on_the_phone);
		String st6 = getResources().getString(
				R.string.The_other_party_did_not_answer);
		String st7 = getResources().getString(R.string.did_not_answer);
		String st8 = getResources().getString(R.string.Has_been_cancelled);
		switch (callingState) {
		case NORMAL:
			txtBody = new EMTextMessageBody(st1 + callDruationText);
			break;
		case REFUESD:
			txtBody = new EMTextMessageBody(st2);
			break;
		case BEREFUESD:
			txtBody = new EMTextMessageBody(st3);
			break;
		case OFFLINE:
			txtBody = new EMTextMessageBody(st4);
			break;
		case BUSY:
			txtBody = new EMTextMessageBody(st5);
			break;
		case NORESPONSE:
			txtBody = new EMTextMessageBody(st6);
			break;
		case UNANSWERED:
			txtBody = new EMTextMessageBody(st7);
			break;
		default:
			txtBody = new EMTextMessageBody(st8);
			break;
		}
		// 设置扩展属性
		if (callType == 0)
			message.setAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, true);
		else
			message.setAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, true);

		// 设置消息body
		message.addBody(txtBody);
		message.setMsgId(msgid);
		message.setStatus(Status.SUCCESS);

		// 保存
		EMClient.getInstance().chatManager().saveMessage(message);
	}

	enum CallingState {
		CANCED, NORMAL, REFUESD, BEREFUESD, UNANSWERED, OFFLINE, NORESPONSE, BUSY
	}
}
